package com.situ.company.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.situ.company.entity.Department;
import com.situ.company.entity.Role;
import com.situ.company.entity.User;
import com.situ.company.service.IDepartmentService;
import com.situ.company.service.IRoleService;
import com.situ.company.service.IUserRoleService;
import com.situ.company.service.IUserService;
import com.situ.company.service.impl.DepartmentServiceImpl;
import com.situ.company.service.impl.RoleServiceImpl;
import com.situ.company.service.impl.UserRoleServiceImpl;
import com.situ.company.service.impl.UserServiceImpl;
import com.situ.company.util.JSONResult;
import com.situ.company.util.JSONUtil;
import com.situ.company.vo.PageInfo;

@WebServlet("/user") 
public class UserServlet extends HttpServlet{
	private IUserService userService = new UserServiceImpl();
	private IDepartmentService departmentService = new DepartmentServiceImpl();
	private IRoleService roleService = new RoleServiceImpl();
	private IUserRoleService userRoleService = new UserRoleServiceImpl();

	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		System.out.println("UserServlet.service()");
		String method = req.getParameter("method");
		if (method == null || "".equals(method)) {
			method = "selectByPage";
		}
		switch (method) {
		case "selectAll":
			selectAll(req, resp);
			break;
		case "selectByPage":
			selectByPage(req, resp);
			break;
		case "deleteById":
			deleteById(req, resp);
			break;
		case "selectLayuiByPage":
			selectLayuiByPage(req, resp);
			break;
		case "deleteAll":
			deleteAll(req, resp);
			break;
		case "getUserInsertPage":
			getUserInsertPage(req, resp);
			break;
		case "insert":
			insert(req, resp);
			break;
		case "getUserUpdatePage":
			getUserUpdatePage(req, resp);
			break;
		case "update":
			update(req, resp);
			break;
		default:
			break;
		}
	}

	private void update(HttpServletRequest req, HttpServletResponse resp) {
		String id = req.getParameter("id");
		String name = req.getParameter("name");
		String age = req.getParameter("age");
		String password = req.getParameter("password");
		String email = req.getParameter("email");
		String departmentId = req.getParameter("departmentId");
		String[] roleIds = req.getParameterValues("roleIds");
		User user = new User(Integer.parseInt(id), name, password, email, Integer.parseInt(age), Integer.parseInt(departmentId));
		boolean isSuccess = userService.update(user, roleIds);
		
		JSONResult jsonResult = null;
		if (isSuccess) {
			jsonResult = new JSONResult(JSONResult.OK, "修改成功");
		} else {
			jsonResult = new JSONResult(JSONResult.ERROR, "修改失败");
		}
		JSONUtil.obj2Json(jsonResult, resp);
	}

	private void getUserUpdatePage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String id = req.getParameter("id");
		User user = userService.selectById(Integer.parseInt(id));
		req.setAttribute("user", user);
		List<Department> departmentList = departmentService.selectAll();
		req.setAttribute("departmentList", departmentList);
		
		// 根据user的id去user_role表里面查出这个用户所有的角色id
		List<Integer> roleIds = userRoleService.selectRoleIdByUserId(Integer.parseInt(id));
		
		List<Role> roleList = roleService.selectAll();
		for (Role role : roleList) {
			for (Integer roleId : roleIds) {
				if (role.getId() == roleId) {
					role.setChecked(true);
				}
			}
		}
		
		req.setAttribute("roleList", roleList);
		
		req.getRequestDispatcher("/user_update.jsp").forward(req, resp);
	}

	private void insert(HttpServletRequest req, HttpServletResponse resp) {
		String name = req.getParameter("name");
		String age = req.getParameter("age");
		String password = req.getParameter("password");
		String email = req.getParameter("email");
		String departmentId = req.getParameter("departmentId");
		String[] roleIds = req.getParameterValues("roleIds");
		User user = new User(name, password, email, Integer.parseInt(age), Integer.parseInt(departmentId));
		boolean isSuccess = userService.insert(user, roleIds);
		
		JSONResult jsonResult = null;
		if (isSuccess) {
			jsonResult = new JSONResult(JSONResult.OK, "添加成功");
		} else {
			jsonResult = new JSONResult(JSONResult.ERROR, "添加失败");
		}
		JSONUtil.obj2Json(jsonResult, resp);
	}

	private void getUserInsertPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		List<Department> departmentList = departmentService.selectAll();
		req.setAttribute("departmentList", departmentList);
		
		List<Role> roleList = roleService.selectAll();
		req.setAttribute("roleList", roleList);
		
		req.getRequestDispatcher("/user_insert.jsp").forward(req, resp);
	}

	private void deleteAll(HttpServletRequest req, HttpServletResponse resp) {
		//ids[]: 48
		//ids[]: 49
		String[] ids = req.getParameterValues("ids[]");
		boolean isSuccess = userService.deleteAll(ids);
		
		JSONResult jsonResult = null;
		if (isSuccess) {
			jsonResult = new JSONResult(JSONResult.OK, "批量删除成功");
		} else {
			jsonResult = new JSONResult(JSONResult.ERROR, "批量删除失败");
		}
		JSONUtil.obj2Json(jsonResult, resp);
	}

	private void selectLayuiByPage(HttpServletRequest req, HttpServletResponse resp) {
		//   /user?method=selectLayuiByPage&page=1&limit=10
		String page = req.getParameter("page");
		String limit = req.getParameter("limit");
		String name = req.getParameter("name");
		
//		List<User> list = new ArrayList<User>();
//		User user1 = new User(1, "张三1", "123", "fdfd", 23);
//		User user2 = new User(2, "张三2", "123", "fdfd", 23);
//		User user3 = new User(3, "张三3", "123", "fdfd", 23);
//		User user4 = new User(4, "张三4", "123", "fdfd", 23);
//		list.add(user1);
//		list.add(user2);
//		list.add(user3);
//		list.add(user4);
		
//		Map<String, Object> map = new HashMap<String, Object>();
//		map.put("code", 0);
//		map.put("msg", "");
//		map.put("count", 1000);
//		map.put("data", list);
//		JSONUtil.obj2Json(map, resp);
		
//		JSONResult jsonResult = new JSONResult(JSONResult.OK, "", list, 1000);
//		JSONUtil.obj2Json(jsonResult, resp);
		
		JSONResult jsonResult = userService.selectLayuiByPage(name, Integer.parseInt(page), Integer.parseInt(limit));
		JSONUtil.obj2Json(jsonResult, resp);
	}

	private void deleteById(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String id = req.getParameter("id");
		boolean isSuccess = userService.deleteById(Integer.parseInt(id));
		//resp.sendRedirect(req.getContextPath() + "/user");
		
		JSONResult jsonResult = null;
		if (isSuccess) {
			jsonResult = new JSONResult(JSONResult.OK, "删除成功");
		} else {
			jsonResult = new JSONResult(JSONResult.ERROR, "删除失败");
		}
		JSONUtil.obj2Json(jsonResult, resp);
	}

	private void selectByPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// /company/user?method=selectByPage&pageNo=1&pageSize=5
		String pageNo = req.getParameter("pageNo");
		String pageSize = req.getParameter("pageSize");
		if (pageNo == null || "".equals(pageNo)) {
			pageNo = "1";
		}
		if (pageSize == null || "".equals(pageSize)) {
			pageSize = "5";
		}
		PageInfo pageInfo = userService.selectByPage(Integer.parseInt(pageNo), Integer.parseInt(pageSize));
		System.out.println(pageInfo);
		req.setAttribute("pageInfo", pageInfo);
		req.getRequestDispatcher("/user_page_list.jsp").forward(req, resp);
	}

	private void selectAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		List<User> list = userService.selectAll();
		req.setAttribute("list", list);
		req.getRequestDispatcher("/user_list.jsp").forward(req, resp);
	}

	private void selectAll1(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		List<User> list = new ArrayList<User>();
		User user1 = new User(1, "张三1", "123", "fdfd", 23);
		User user2 = new User(2, "张三2", "123", "fdfd", 23);
		User user3 = new User(3, "张三3", "123", "fdfd", 23);
		User user4 = new User(4, "张三4", "123", "fdfd", 23);
		list.add(user1);
		list.add(user2);
		list.add(user3);
		list.add(user4);
		req.setAttribute("list", list);
		req.getRequestDispatcher("/user_list.jsp").forward(req, resp);
	}
}
